package com.hc.javabasestudyproject.juc.copyonwrite;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: huangcheng
 * @CreateTime: 2024-04-10  14:10
 * @Description:
 * @Version: 1.0
 */
public class CopyOnWriteArrayListTest {

    public static void main(String[] args) {
        CopyOnWriteArrayList<String> copyOnWriteArr = new CopyOnWriteArrayList<>();
        ArrayList<String> arrayList = new ArrayList<>();
        copyOnWriteArr.add("zhangsan");
        copyOnWriteArr.add("武松");
        arrayList.add("zhangsan");
        arrayList.add("武松");

        copyOnWriteArr.remove("");
        // CopyOnWriteArrayList的迭代器不支持remove方法
        Iterator<String> iterator = copyOnWriteArr.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if (next.equals("zhangsan")) {
                iterator.remove();
            }
        }
        copyOnWriteArr.forEach(System.out::println);
        Iterator<String> arraylistIterator = arrayList.iterator();
        while (arraylistIterator.hasNext()) {
            String e = arraylistIterator.next();
            if (e.equals("zhangsan")) {
                arraylistIterator.remove();
            }
        }
        arrayList.forEach(System.out::println);
    }
}
